$("#roadLineModal4Audit input").attr("readonly", true);
$("#roadLineModal4Audit select").attr("disabled", true);
$(function () {
    gMainDT = $("#mainDataTable").DataTable({
        ajax: {
            url: "../rds_roadLineController/getRoadLine4DT",
            type: "POST",
            data: function (d) {
                d.search = $('#mainDataTable').DataTable().search(this.value);
                d.actorIds = gUserLoginResult.userActors;
                d.orgId = gUserLoginResult.orgId;
                d.audit = true;
                d.startRoad = "";
                d.endRoad = "";
                d.roadName = "";
                d.saveState = "";
            }
        },
        columns: [{
            data: "roadLineId",
            render: function (data, type, row, meta) {
                var checkId = "checkbox" + (parseInt(meta.row) + 1);
                return "<input type='checkbox' value='" + data + "' id='" + checkId + "'>";
            }
        }, {
            data: null,
            targets: 0
        }, {
            data: "roadLinePhoto",
            render: function (data, type, row) {
                var pic = data.split(",");
                if (pic[0])
                    return "<img src='http://hldoss.oss-cn-hangzhou.aliyuncs.com/" + pic[0] + "' style='width: 50px; height: 40px; cursor: pointer' " +
                        "onclick='$.showPicModal(\"" + data + "\")'>";
                else
                    return "<img src='../images/default_photo.png' style='width: 50px; height: 40px; cursor: pointer'>";
            }
        }, {
            data: null,
            render: function (data, type, row) {
                return row.roadLineProvince + " " + row.roadLineCity + " " + row.roadLineArea;
            }
        }, {
            data: "roadLineRoadName"
        }, {
            data: null,
            render: function (data, type, row) {
                return row.roadLineStartRoad + "——" + row.roadLineEndRoad;
            }
        }, {
            data: "roadLineOrgName"
        }, {
            data: "roadLineCreatorName"
        }, {
            data: "roadLineInstallTime"
        }, {
            data: "roadLineId",
            render: function (data, type, row) {
                var buttonOperation = "";
                var buttonAudit = "<button class='btn btn-primary btn-xs btn-flat' " +
                    "onclick='$.auditRoadLine(" + data + ")' title='审核'>" +
                    "<i class='glyphicon glyphicon-check'></i></button> ";
                gCheck == 0 ? buttonOperation : buttonOperation += buttonAudit;

                buttonOperation += "<button class='btn btn-primary btn-xs btn-flat' " +
                    "onclick='$.showRoadLine(" + JSON.stringify(row) + ")' title='查看'>" +
                    "<i class='glyphicon glyphicon-eye-open'></i></button> ";

                var buttonDelete = "<button class='btn btn-primary btn-xs btn-flat' " +
                    "onclick='$.deleteRoadLine(" + data + ")' title='删除'>" +
                    "<i class='fa fa-minus-square'></i></button>";
                gDelete == 0 ? buttonOperation : buttonOperation += buttonDelete;

                return buttonOperation;
            }
        }],
        dom: "<'row'<'col-md-4'l><'#mainToolbar.col-md-8'>r>t<'row'<'col-md-5'i><'col-md-7'p>>",
        initComplete: function () {
            // 下拉工具栏按钮的权限
            $("#mainToolbar").empty();
            var strToolBar = "<div class='pull-right'><div class='btn-group'>" +
                "<button type='button' class='btn btn-primary btn-sm btn-flat'><i class='fa fa-cog'></i> 操作</button>" +
                "<button type='button' class='btn btn-primary btn-sm btn-flat dropdown-toggle' " +
                "data-toggle='dropdown' aria-haspopup='true' aria-expanded='false'>" +
                "<span class='caret'></span><span class='sr-only'>切换下拉</span></button>" +
                "<ul class='dropdown-menu'>" +
                "<li>" +
                "<button href='#' class='btn btn-primary btn-sm btn-flat btn-block' " +
                "onclick='gMainDT.ajax.reload(null, false);'><i class='fa fa-refresh'></i> 刷新</button>" +
                "</li>";

            var buttonAudit = "<li><button href='#' class='btn btn-primary btn-sm btn-flat btn-block' " +
                "onclick='$.auditRoadLine(0)'><i class='fa fa-plus-square'></i> 审核</button></li>";
            gCheck == 0 ? strToolBar : strToolBar += buttonAudit;

            var buttonDelete = "<li><button href='#' class='btn btn-primary btn-sm btn-flat btn-block' " +
                "onclick='$.deleteRoadLine(0)'><i class='fa fa-minus-square'></i> 删除</button></li>";
            gDelete == 0 ? strToolBar : strToolBar += buttonDelete;

            $("#mainToolbar").append(strToolBar);
        },
        processing: true,
        serverSide: true,
        ordering: false,
        select: false,
        autoFill: true,
        displayLength: 20,
        lengthMenu: [[5, 10, 15, 20, 50, 100, 150, -1], [5, 10, 15, 20, 50, 100, 150, "All"]],
        language: {
            url: "zh_cn.json"
        },
        fnDrawCallback: function () {// 序号自增回调函数
            var api = this.api();
            var startIndex = api.context[0]._iDisplayStart;
            api.column(1).nodes().each(function (cell, i) {
                cell.innerHTML = startIndex + i + 1;
            });
        }
    });

});

$.fn.searchDT = function () {
    gMainDT.search().draw();
};

$.checkChange = function () {
    var isChecked = $("#batchCheck").prop('checked');
    $("input:checkbox[id^='checkbox']").prop("checked", isChecked);
};

$.showPicModal = function (picList) {
    dex = 0;//图片轮转置0
    picList = picList.split(",");
    var pic = $('.img-content');
    pic.html("").css("top", "0px");
    for (var i in picList) {
        if (picList[i] != "")
            pic.append("<img src='http://hldoss.oss-cn-hangzhou.aliyuncs.com/" + picList[i] + "'>")
    }
    $(".img-content img[src]:first").show();
    $("#showPicList").modal({
        backdrop: 'static',
        keyboard: false,
        show: true
    })
};

$.auditRoadLine = function (roadLineId) {
    var count = 1;
    if (roadLineId == 0) {
        count = $("input:checkbox[id^='checkbox']:checked").length;
        if (count == 0) {
            $.alert("请选择要审核的数据", 2);
            return;
        } else {
            roadLineId = "";
            $("input:checkbox[id^='checkbox']:checked").each(function () {
                roadLineId += $(this).val() + ",";
            });
            roadLineId = roadLineId.substring(0, roadLineId.length - 1);
        }
    }
    bootbox.confirm({
        buttons: {
            confirm: {
                label: '<i class="fa fa-check"></i> 确定',
                className: 'btn btn-primary btn-flat'
            },
            cancel: {
                label: '<i class="fa fa-close"></i> 取消',
                className: 'btn btn-warning btn-flat pull-left'
            }
        },
        message: '<i class="fa fa-question-circle-o"></i> <b class="customer-title">确定审核通过该' + count + '条数据吗?</b>',
        callback: function (result) {
            if (result) {
                $.ajax({
                    url: '../rds_roadLineController/auditRoadLine',
                    type: 'post',
                    dataType: 'json',
                    data: {
                        "roadLineIds": roadLineId
                    },
                    success: function (msg) {
                        if (msg != null && typeof (msg) != "undefined") {
                            if (msg.isSuccess == true) {
                                $.alert("数据审核通过成功", 1);
                                gMainDT.ajax.reload(null, false);
                            } else {
                                $.alert("数据审核通过失败", 2);
                            }
                        } else {
                            $.alert('系统错误，请联系管理员', 2);
                        }
                    },
                    error: function () {
                        $.alert('系统错误，请联系管理员', 2);
                    }
                });
            }
        }
    });
};

$.deleteRoadLine = function (roadLineId) {
    var count = 1;
    if (roadLineId == 0) {
        count = $("input:checkbox[id^='checkbox']:checked").length;
        if (count == 0) {
            $.alert("请选择要删除的数据", 2);
            return;
        } else {
            roadLineId = "";
            $("input:checkbox[id^='checkbox']:checked").each(function () {
                roadLineId += $(this).val() + ",";
            });
            roadLineId = roadLineId.substring(0, roadLineId.length - 1);
        }
    }
    bootbox.confirm({
        buttons: {
            confirm: {
                label: '<i class="fa fa-check"></i> 确定',
                className: 'btn btn-primary btn-flat'
            },
            cancel: {
                label: '<i class="fa fa-close"></i> 取消',
                className: 'btn btn-warning btn-flat pull-left'
            }
        },
        message: '<i class="fa fa-question-circle-o"></i> <b class="customer-title">确定删除该' + count + '条数据吗?</b>',
        callback: function (result) {
            if (result) {
                $.ajax({
                    url: '../rds_roadLineController/deleteRoadLine',
                    type: 'post',
                    dataType: 'json',
                    data: {
                        "roadLineIds": roadLineId
                    },
                    success: function (msg) {
                        if (msg != null && typeof (msg) != "undefined") {
                            if (msg.isSuccess == true) {
                                $.alert("数据删除成功", 1);
                                gMainDT.ajax.reload(null, false);
                            } else {
                                $.alert("数据删除失败", 2);
                            }
                        } else {
                            $.alert('系统错误，请联系管理员', 2);
                        }
                    },
                    error: function () {
                        $.alert('系统错误，请联系管理员', 2);
                    }
                });
            }
        }
    });
};

$.showRoadLine = function (jsonData) {
    $.ajax({
        url: "../rds_roadLineController/getCoordinatesById",
        type: "post",
        data: {roadLineId: jsonData.roadLineId, type: 1},
        success: function (msg) {
            if (msg != null && typeof msg != "undefined") {
                if (msg.isSuccess == true) {
                    var points = "", josnData = msg.jsonData;
                    console.log(josnData);
                    josnData.forEach(function (value, index, array) {
                        points += value.coordinateLongitude + "," + value.coordinateLatitude + ";";
                    });
                    $("#coordinate").val(points);
                    $("#roadLinePoints").val(points);
                }
            }
        }
    });

    dex = 0;//图片轮转
    var picList = jsonData.roadLinePhoto.split(",");
    var $pic = $(".pic_list"), flag = 0;
    $pic.html("").css("top", "0px");
    for (var i in picList) {
        if (picList[i] != "") {
            flag = 1;
            $pic.append("<img src='http://hldoss.oss-cn-hangzhou.aliyuncs.com/" + picList[i] + "'>");
        }
    }
    if (flag == 0)
        $pic.append("<img src='../images/default_photo.png'>");


    $("#projectNumber").val(jsonData.projectNumber);
    $("#roadLineProvince").val(jsonData.roadLineProvince);
    $("#roadLineCity").val(jsonData.roadLineCity);
    $("#roadLineArea").val(jsonData.roadLineArea);
    $("#roadLineRoadName").val(jsonData.roadLineRoadName);
    $("#roadLineStartRoad").val(jsonData.roadLineStartRoad);
    $("#roadLineEndRoad").val(jsonData.roadLineEndRoad);
    $("#roadLineBrand").val(jsonData.roadLineBrand);
    $("#roadLineInstallOrg").val(jsonData.roadLineInstallOrg);
    $("#roadLineInstallTime").val(jsonData.roadLineInstallTime);
    $("#roadLineRemark").val(jsonData.roadLineRemark);
    $("#roadLineOrgName").val(jsonData.roadLineOrgName);
    // $("#roadLinePhotoImg").attr("src", jsonData.roadLinePhoto);
    $("#roadLinePeriod").val(jsonData.roadLinePeriod);
    $("#roadLineServiceLife").val(jsonData.roadLineServiceLife);
    $("#roadLineMainOrgName").val(jsonData.roadLineMainOrgName);

    $("#roadLineType").val(jsonData.roadLineType);
    setHtml(jsonData.roadLineType);
    $("#roadLineGraphic").val(jsonData.roadLineGraphic);
    $("#roadLineId").val(jsonData.roadLineId);
    $("#roadLineCreatorName").val(jsonData.roadLineCreatorName);
    $("#roadLineCreatorId").val(jsonData.roadLineCreatorId);
    $("#roadLineModifierId").val(gUserLoginResult.userId);
    $("#roadLineOrgId").val(jsonData.roadLineOrgId);
    $("#roadLinePhoto").val(jsonData.roadLinePhoto);


    $("#roadLineModal4Audit").modal({
        backdrop: 'static',
        keyboard: false,
        show: true
    });
};


function setHtml(type) {
    switch (type) {
        case 0:
            type = "指示";
            appendHtml(type);
            break;
        case 1:
            type = "禁止";
            appendHtml(type);
            break;
        case 2:
            type = "警告";
            appendHtml(type);
            break;
        case 3:
        case 4:
            console.log("应藏")
            $("#graphic").fadeOut();
            break;
    }
}

function appendHtml(type) {
    $("#graphic").fadeIn();
    $("#roadLineGraphic").html(
        '<option value="0">纵向' + type + '标线</option>' +
        '<option value="1">横向' + type + '标线</option>' +
        '<option value="2">其他' + type + '标线</option>'
    );
}

$.showMap = function (jsonData) {
    $("#modalMapRoad").hide();
    var url = "management/roadLineMap.html";
    url += "?coordinate=" + $("#coordinate").val() + "&type=3";//通过这种方式传参数
    $("#myMap").attr("src", url);
    $("#modal-map").modal({
        backdrop: 'static',
        keyboard: false,
        show: true
    })
};

$("#myBtn").on("click", function () {
    $.showMap($("#roadLineId").val());
});

$.showAuditRoadLine = function (roadLineId) {
    $.auditRoadLine(roadLineId);
    $.clearModal($("#roadLineForm"), $("#roadLineModal4Audit"))
};


/************图片轮转实现1*************************/
function next() {
    var $picList = $(".pic_list");
    var length = $picList.find("img").length;
    if (dex == length - 1)
        dex = 0;
    else
        dex += 1;
    $picList.css("top", -280 * dex + "px");
}

function prev() {
    var $picList = $(".pic_list");
    var length = $picList.find("img").length;
    if (dex == 0)
        dex = length - 1;
    else
        dex -= 1;
    $picList.css("top", -280 * dex + "px");
}

$(".next").click(_debounce(next, 200));

$(".prev").click(_debounce(prev, 200));

/************图片轮转实现2*************************/
function next2() {
    var $imgList = $(".img-content img[src]");
    $imgList.eq(dex).hide();
    if (dex == $imgList.length - 1)
        dex = 0;
    else dex++;
    $imgList.eq(dex).fadeIn()
}

function prev2() {
    var $imgList = $(".img-content img[src]");
    $imgList.eq(dex).hide();
    if (dex == 0)
        dex = $imgList.length - 1;
    else dex--;
    $imgList.eq(dex).fadeIn()
}


var $next2=_debounce(next2, 200)
var $prev2=_debounce(prev2, 200)

$(".next2").click($next2);

$(".prev2").click($prev2);